﻿using GetTogether.Studio.Logging;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class logging_ViewLog : System.Web.UI.Page
{
    string soapRegx = @"Soap Logging Start:
.*?
Date Time:(?<DateTime>.*?)
Action:(?<Action>.*?)
URL:(?<URL>.*?)
Action Type:(?<ActionType>.*?)
Soap Start:
(?<SOAP>.*?)
Soap End
Soap Logging End";
    protected void Page_Load(object sender, EventArgs e)
    {
        string file = Request["file"];
        if (!string.IsNullOrEmpty(file))
        {
            GetTogether.Net.Extension.WebClient wc = new GetTogether.Net.Extension.WebClient();
            wc.Encoding = System.Text.Encoding.UTF8;
            wc.Timeout = int.MaxValue / 1000;
            string log = wc.DownloadString(file);

            if (Request["view-type"] == "1")
            {
                SoapLoggings soapLogs = GetSoapLoggings(log);
                if (soapLogs != null && soapLogs.Count > 0)
                {
                    soapLogs.SortBy("RequestDateTime", false);
                    SoapList1.SoapLoggings = soapLogs;
                }
                else
                {
                    Response.Clear();
                    Response.ContentType = "text/plain";
                    Response.Write(log);
                    Response.End();
                }
            }
            else
            {
                Response.ContentType = "text/plain";
                Response.Write(log);
                Response.End();
            }
        }
        else
        {
            Response.Clear();
            Response.Write("Invalid parameter.");
            Response.End();
        }
    }

    public SoapLoggings GetSoapLoggings(string log)
    {
        if (string.IsNullOrEmpty(log)) return null;
        System.Text.RegularExpressions.MatchCollection matchs = new System.Text.RegularExpressions.Regex(soapRegx, System.Text.RegularExpressions.RegexOptions.Singleline).Matches(log);
        SoapLoggings soapLogs = new SoapLoggings();
        foreach (System.Text.RegularExpressions.Match m in matchs)
        {
            DateTime dateTime = DateTime.MinValue;
            DateTime.TryParse(m.Groups["DateTime"].Value, out dateTime);
            string soap = m.Groups["SOAP"].Value;
            string ActionType = m.Groups["ActionType"].Value;
            string url = m.Groups["URL"].Value;
            if (ActionType == "Request")
            {
                SoapLoggings.SoapLogging sl = new SoapLoggings.SoapLogging();
                sl.Request = soap;
                sl.RequestDateTime = dateTime;
                sl.Url = url;
                soapLogs.Add(sl);
            }
            else if (ActionType == "Response" && soapLogs.Count > 0 && string.IsNullOrEmpty(soapLogs[soapLogs.Count - 1].Response))
            {
                SoapLoggings.SoapLogging prevSoapLog = soapLogs[soapLogs.Count - 1];
                prevSoapLog.Response = soap;
                prevSoapLog.ResponseDateTime = dateTime;
                prevSoapLog.Url = url;
                prevSoapLog.ElaspedSeconds = prevSoapLog.ResponseDateTime.Subtract(prevSoapLog.RequestDateTime).TotalSeconds;
            }
        }
        return soapLogs;
    }
}